// Copyright 2010 Google Inc. All Rights Reserved.

package com.google.zahra.test;

import com.google.zahra.testmanager.TestManager;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;

/**
 * The SystemTester is used to test the system as a whole. It simply adds test
 * cases to the TestManager.
 * 
 * @author arthurkok@google.com (Your Name Here)
 *
 */
public class SystemTester {
  
  private TestManager m_testManager;
  
  /**
   * Constructor.
   * 
   * @param numOfTests Number of tests to add
   * @param speedOfTests Speed at which tests will be added
   * @throws IOException 
   */
  public SystemTester(int numOfTests, int speedOfTests, String pythonFileLoc) throws IOException {
    
    // Creates a TestManager that contains a distributed queue that is used by
    // the Master to get pending tests
    m_testManager = new TestManager();
    Random generator = new Random();
    String python = "";
    
    BufferedReader in = new BufferedReader(new FileReader(pythonFileLoc));
    String str;
    while  ((str = in.readLine()) != null) {
      python += str + "\n";
    }
    
    int i = 0;
    
    // Loop for numOfTest, or infinity if numOfTest is zero (0)
    while(i < numOfTests || numOfTests == 0) {
      
      int os = generator.nextInt(7) + 1;    
            
      m_testManager.addTestCase(python, "TEST NUMBER " + i++, 7, 1);
      
      try {
        Thread.sleep(speedOfTests);
      } catch (InterruptedException e) {
        Thread.interrupted();
        e.printStackTrace();
      }      
    }
    
    in.close();
  }

  public static void main(String args[]) {    
    
    // Get the arguments
    int numOfTests = Integer.parseInt(args[0]);
    int speedOfTests = Integer.parseInt(args[1]);
    String pythonFileLoc = args[2];
    
    if (speedOfTests == 0) {
      speedOfTests = 1000;
    }
    
    try {
      SystemTester systemTester = new SystemTester(numOfTests, speedOfTests, pythonFileLoc);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}
